package org.noote.test_widgets;

import javax.microedition.khronos.opengles.GL10;

import org.noote.libs.sogle.Light;
import org.noote.libs.sogle.Vector3D;
import org.noote.libs.sogle.helper.LineMesh_Grid;
import org.noote.libs.widget.WidgetEvent_OnChangeItem;
import org.noote.libs.widget.WidgetEvent_OnClick;
import org.noote.libs.widget.WidgetEvent_OnTouch;
import org.noote.libs.widget.WidgetRenderer;
import org.noote.libs.widget.WidgetTexturePack;
import org.noote.libs.widget.WidgetTheme;
import org.noote.libs.widget.Widget_Carousel;
import org.noote.libs.widget.Widget_CarouselItem;
import org.noote.libs.widget.Widget_DirectionPad;
import org.noote.libs.widget.Widget_Label;
import org.noote.libs.widget.Widget_LabelPad;
import org.noote.libs.widget.Widget_Window;
import org.noote.libs.widget.Widget_ZoomPad;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class TestWidget_Activity extends Activity {

	static final String TAG = "TestWidget_Activity";
	
	private static final int MESSAGE_UI_TOAST = 0;

    Widget_LabelPad _transmit_pad, _connection_pad, _setting_pad;
    Widget_DirectionPad _view_dir_pad;
    Widget_ZoomPad _view_zoom_pad;
    Widget_Carousel _animation_carousel;
    Widget_Label _label_message, _label_guiFPS;
    
    Widget_DirectionPad _spidy_dir_pad;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new TestGLSurfaceView(getApplicationContext()));   
    }
    
    class TestRenderer extends WidgetRenderer
    {
    	WidgetTexturePack _myAnimationIcons = new WidgetTexturePack();
    	Light _light=null;
    	
        public TestRenderer(Context context)
        {
        	super(context);
        }
        
        @Override
        public void onCreateScene(GL10 gl)
    	{
    		super.onCreateScene(gl);
    		
            lighting(false);
            
            setBackGroundColor(0.5f, 0.5f, 0.5f);

            _camera.setLookAt(new Vector3D(0, 20, 50), new Vector3D(0, 20, 0), 0);
            
            LineMesh_Grid floor = new LineMesh_Grid(50.0f, 50.0f, 20, 20);
            //Mesh_Plane floor = new Mesh_Plane(50.0f, 50.0f, 10, 10);
            _listObjects.add(floor);
            
            _light = new Light();
            _light.enable(true);
            _listLights.add(_light);
            
            _label_message = _dekstop.createLabel();
            _label_message.setWorkSpace(getWidth()/2, 32, 256, 32);
            _label_message.setTextColor(0, 1, 1);
            _dekstop.add(_label_message);
            
            // a carroussel with all animation loops
            int iAnimCount=0;
            _myAnimationIcons.create(gl, 2);
            _animation_carousel = _dekstop.createCarousel();
            _animation_carousel.setRightDocked();
            for(int ai=0; ai<2; ai++)
            {
            	_myAnimationIcons.loadImageFromResource(_context, R.drawable.no_anim, gl, iAnimCount);
            	
            	Widget_CarouselItem item = _animation_carousel.createCarouselItem();
            	item.setText("Anim "+(ai+1));
            	item.setLongData(iAnimCount);
            	item.setIcon(_myAnimationIcons.getTextureID(iAnimCount));
            	_animation_carousel.add(item);
            	
            	iAnimCount++;
            }
            _animation_carousel.setOnChangeItem(new WidgetEvent_OnChangeItem()
            {
				@Override
				public boolean onSelectedItem(Widget_Window window, Widget_Window item) {
					showToast("Select item");
					return true;
				}
            });
            _animation_carousel.selectItem(0);
            _dekstop.add(_animation_carousel);

            // directional pad :)
            _view_dir_pad = _dekstop.createDirectionPad();
            _view_dir_pad.setPosition(_view_dir_pad.getWidth()/2, getHeight()-(int)(_view_dir_pad.getHeight()*2.5f));
            _view_dir_pad.setOnTouch(new WidgetEvent_OnTouch()
            {
				@Override
				public boolean onPressed(Widget_Window window) {

					if(_view_dir_pad.getButtonUp())
    	    		{
						showToast("View UP");
    	    		}
    	    		else if(_view_dir_pad.getButtonDown())
    	    		{
    	    			showToast("View DOWN");
    	    		}
    	    		if(_view_dir_pad.getButtonRight())
    	    		{
    	    			showToast("View RIGHT");
    	    		}
    	    		else if(_view_dir_pad.getButtonLeft())
    	    		{
    	    			showToast("View LEFT");
    	    		}
    	    		
					return true;
				}

				@Override
				public boolean onMoved(Widget_Window window) {
					return true;
				}

				@Override
				public boolean onReleased(Widget_Window window) {
					return true;
				}
            });
            _dekstop.add(_view_dir_pad); 	

            // zoom pad :)
            _view_zoom_pad = _dekstop.createZoomPad();
            _view_zoom_pad.setPosition(_view_zoom_pad.getWidth()/2, (int)(_view_zoom_pad.getHeight()*1.5f));
            _view_zoom_pad.setOnTouch(new WidgetEvent_OnTouch()
            {
				@Override
				public boolean onPressed(Widget_Window window) {

	    			if(_view_zoom_pad.getButtonFront())
	    			{
	    				showToast("Press ZOOM_IN");
	    			}
	    			else if(_view_zoom_pad.getButtonBack())
	    			{
	    				showToast("Press ZOOM_OUT");	    	    				
	    			}
    	    		
					return true;
				}

				@Override
				public boolean onMoved(Widget_Window window) {
					return true;
				}

				@Override
				public boolean onReleased(Widget_Window window) {
					return true;
				}
            });
            _dekstop.add(_view_zoom_pad);

            // settings pad :)
            _setting_pad = _dekstop.createLabelPad();
            _setting_pad.setPosition(0, getHeight()-_setting_pad.getHeight());
            _setting_pad.setText("Setting");
            _setting_pad.setSystemIcon(WidgetTheme.ICON_SETTING);
            _setting_pad.setOnClick(new WidgetEvent_OnClick()
            {
				@Override
				public boolean onClick(Widget_Window window) {
					showToast("Press SETTINGS");
					return true;
				}
            });
            _dekstop.add(_setting_pad);

            // connection pad :)
            _connection_pad = _dekstop.createLabelPad();
            _connection_pad.setPosition(_setting_pad.getPositionX()+_setting_pad.getWidth(), getHeight()-_connection_pad.getHeight());
            _connection_pad.setText("Connection");
            _connection_pad.setOff();
            _connection_pad.setOnClick(new WidgetEvent_OnClick()
            {
				@Override
				public boolean onClick(Widget_Window window) {
	    			if(_connection_pad.isOn())
	    				showToast("BT On");
    	    		else
    	    			showToast("BT Off");
					return true;
				}
            });
            _dekstop.add(_connection_pad);
          
            // transmit pad :)
            _transmit_pad = _dekstop.createLabelPad();
            _transmit_pad.setPosition(_connection_pad.getPositionX()+_connection_pad.getWidth(), getHeight()-_transmit_pad.getHeight());
            _transmit_pad.setText("Transmition");
            _transmit_pad.setOff();
            _transmit_pad.enableWindow(false);
            _transmit_pad.setOnClick(new WidgetEvent_OnClick()
            {
				@Override
				public boolean onClick(Widget_Window window) {
    	    		if(_transmit_pad.isOn())
    	    			showToast("Transmit On");
    	    		else
    	    			showToast("Transmit Off");
					return true;
				}
            });
            _dekstop.add(_transmit_pad);
            
            _label_guiFPS = _dekstop.createLabel();
            _label_guiFPS.setTextColor(_dekstop.getTheme().colorLightGray);
            _label_guiFPS.setWorkSpace(_setting_pad.getPositionX()+8, _setting_pad.getPositionY()-16, 128, 32);
            _dekstop.add(_label_guiFPS);

            // directional pad :)
            _spidy_dir_pad = _dekstop.createDirectionPad();
            _spidy_dir_pad.setSize((int)(_spidy_dir_pad.getWidth()*1.5f), (int)(_spidy_dir_pad.getHeight()*1.5f));
            _spidy_dir_pad.setPosition((int)(getWidth()-_animation_carousel.getWidth()-_spidy_dir_pad.getWidth()*1.25f), getHeight()/2-_spidy_dir_pad.getHeight()/2);
            _spidy_dir_pad.setOnTouch(new WidgetEvent_OnTouch()
            {
				@Override
				public boolean onPressed(Widget_Window window) {

					if(_spidy_dir_pad.getButtonUp())
    	    		{
						showToast("Go FRONT");
    	    		}
    	    		else if(_spidy_dir_pad.getButtonDown())
    	    		{
    	    			showToast("Go BACK");
    	    		}
    	    		if(_spidy_dir_pad.getButtonRight())
    	    		{
    	    			showToast("Turn RIGHT");
    	    		}
    	    		else if(_spidy_dir_pad.getButtonLeft())
    	    		{
    	    			showToast("Turn LEFT");
    	    		}
    	    		
					return true;
				}

				@Override
				public boolean onMoved(Widget_Window window) {
					return true;
				}

				@Override
				public boolean onReleased(Widget_Window window) {
					return true;
				}
            });
            _dekstop.add(_spidy_dir_pad); 	
    	}
    	
		@Override
        public void onFrame(float fTimeSlice) {
 
    		_label_guiFPS.setText(String.format("App FPS : %2.1f", _fps.getFPS()));
    	}
    }
	
	class TestGLSurfaceView extends GLSurfaceView {
		
		TestRenderer mRenderer = null;
		
	    public TestGLSurfaceView(Context context) {
	        super(context);
	        
	        mRenderer = new TestRenderer(context);
	        setRenderer(mRenderer);
	        setPreserveEGLContextOnPause(true);
	    }

	    @Override
		public boolean onTouchEvent(final MotionEvent event)
	    {
	        queueEvent(new Runnable(){
	            @Override
				public void run() {
	            	
	    	    	mRenderer.onTouchEvent(event);

	            }});
	        return true;
	    }
	}
    
	public void showToast(String sMsg)
	{
		_UI_Handler.sendMessage(_UI_Handler.obtainMessage(MESSAGE_UI_TOAST, sMsg));
	}
	
	@SuppressLint("HandlerLeak")
	final Handler _UI_Handler = new Handler() {
	    @Override
		public void handleMessage(Message msg) {
	        switch (msg.what) {
	        case MESSAGE_UI_TOAST:
	        {
	        	Toast.makeText(getApplicationContext(), (String)msg.obj, Toast.LENGTH_LONG).show();
	        }
	            break;
	        }
	    }
	};}
